package com.ittx.dzt.ali.health.service;

import cn.hutool.core.util.StrUtil;
import com.ittx.dzt.ali.health.factory.TbSdkClientFactory;
import com.ittx.dzt.ali.health.handler.AbstractBaseAliHealthApiHandler;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.InventoryUploadDTO;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.log.monitor.ExceptionLog;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAlihealthInventoryIncreaseRequest;
import com.taobao.api.request.AlibabaAlihealthInventoryQueryRequest;
import com.taobao.api.response.AlibabaAlihealthInventoryIncreaseResponse;
import com.taobao.api.response.AlibabaAlihealthInventoryQueryResponse;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import open.dto.ali.health.AliHealthInventoryQueryDto;
import org.springframework.stereotype.Service;

/**
 * 增加直发库存
 * <p>
 * <p>
 * cy.inventory.upload.increment
 * alibaba.alihealth.inventory.increase
 * https://open.taobao.com/api.htm?docId=37526&docType=2
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/3/19
 */
@Slf4j
@Service
public class AliHealthInventoryIncreaseService extends AbstractBaseAliHealthApiHandler<InventoryUploadDTO> {
	@Override
	public String getLocalApiMethod() {
		return PlatformApiConstant.InboundInterface.INVENTORY_UPLOAD_INCREMENT;
	}

	@Override
	public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, InventoryUploadDTO dto) {
		StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();
		AlibabaAlihealthInventoryIncreaseResponse rsp = null;
		try {
			TaobaoClient client = TbSdkClientFactory.getTaobaoClient(storeConfig.getSdkUrl(), storeConfig.getAppKey(), storeConfig.getAppSecret());
			AlibabaAlihealthInventoryIncreaseRequest req = new AlibabaAlihealthInventoryIncreaseRequest();
			req.setItemBarCode(dto.getPlatformSpuId());
			req.setQuantity(Long.valueOf(dto.getSyncQty()));
			req.setSupplierId(Long.valueOf(dto.getStoreCode()));
			req.setOutBizCode(platformApiParamDTO.getRequestId());
			log.debug("入参数据：{} 请求参数:{}", JacksonTool.toJson(dto), JacksonTool.toJson(req));
			rsp = client.execute(req, storeConfig.getSessionKey());
			if (!rsp.isSuccess()) {
				String logStr = StrUtil.format("ErrorCode:{},Msg:{},SubCode:{},subMsg:{}", rsp.getErrorCode(), rsp.getMsg()
						, rsp.getSubCode(), rsp.getSubMsg());
				log.error(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), platformApiParamDTO.getApi(), logStr);
				return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003, StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(),
						this.getPlatformCode(), platformApiParamDTO.getApi(), logStr))).addMsg(rsp.getSubMsg());
			}
			return Result.success(rsp.getResult());
		} catch (Exception e) {
			String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), platformApiParamDTO.getApi(), e.getMessage());
			ExceptionLog.error(e, msg);
			log.error(msg);
			return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003, msg));
		}
	}
}
